
#include <pic18.h>
#include "my_macros.h"

signed int errno;
/* 
PC_Transceiver(PCT) is *transparent(pipe) device between PC and RF unit.
This means that the data was send from PC without change will be send to RF unit by RF transmitter (see RF command in the table below).

 */
const unsigned long Exp[9] = 
{
	10,
	100,
	1000,
	10000,
	100000,
	1000000,
	10000000,
	100000000,
	1000000000
};

signed char MemCmp ( unsigned char* s1, unsigned char* s2, unsigned char BytesNumber )
{
	register signed int Result;
	
	errno = 0;
	
	while( BytesNumber-- )
	{
		if ( (Result = *s1++ - *s2++) ) 
			break;
		errno++;
	}

	return Result; 
}

/* char* FlipFlop ( void* Dest_Str ) */
/* { */
/* 	char* Str = Dest_Str; */
/* 	unsigned char tmp, p1 = 0 , p2 = StrLen(Str)-1; */
/* 	unsigned char c = p2>>1; */
	
/* 	while ( 1 ) */
/* 	{ */
/* 		tmp = Str[p1]; */
/* 		Str[p1] = Str[p2]; */
/* 		Str[p2] = tmp; */
	
/* 		if ( p1 < c ) */
/* 		{ */
/* 			p1++; */
/* 			p2--; */
/* 		}	 */
/* 		else */
/* 		{ */
/* 			return Dest_Str; */
/* 		} */
/* 	} */
/* } */

void FlipFlopN ( char* Str, unsigned char Len )
{
	unsigned char tmp, p1 = 0 , p2 = Len-1;
	unsigned char c = p2>>1;
	
	while ( 1 )
	{
		tmp = Str[p1];
		Str[p1] = Str[p2];
		Str[p2] = tmp;
	
		if ( p1 < c )
		{
			p1++;
			p2--;
		}	
		else
		{
			break;
		}
	}
}


/* return long, in errno variable offset up first number of the string or (-1) if errors*/
signed long StrToLong( const char* s )
{
	register char	c;
	register long	a;
	register unsigned char sign;
	
	if ( s == NULL )
	{
		errno = -1;
		return (-1);
	}


	//gpStr = NULL;
	
	errno = 0;
	a = 0;
	sign = 0;

  skipws:
	
	c = *s;
	if( ( c < '0' || c > '9' ) && ( c != '-' ) ) 
	{
		if ( c == 0 ) //end of string
		{
			//errno = NO_NUMBER;
			errno = -2;				  /* number not found */
			return (-1);
		}
		a = 0;
		sign = 0;
		s++;
		goto skipws;
	}

	if(c == '-') 
	{
		sign++;
		s++;
		goto skipws;
	} 
	else 
		if(c == '+')
		{
			s++;
			goto skipws;			
		}

  conv:
	c = *s;
	if ( c == '.' )				  /* skip a point */
	{
		s++;
		goto conv;
	}

	if( c >= '0' && c <= '9') 
	{
		a = a*10L + (c - '0');
		s++;
		goto conv;
	}

	//gpStr = s;

	if(sign)
		return -a;
	
	return a;
}

signed char StrFindChar ( unsigned char *Buffer, unsigned char Ch )
{
	unsigned char num;
	
	for ( num = 0; Buffer[num] || num < 127; num++)
	{
		if ( Buffer[num] == Ch )
		{
			return num;
		}
	}
	
	return -1;
}

unsigned char* MemCpy ( unsigned char *m1, const unsigned char *m2, unsigned char ByteNumber )
{
	unsigned char i;
	
	for ( i = 0; i < ByteNumber; i++ )
	{
		m1[i] = m2[i];
	}

	return m1; 
}

char* StrCat( char* to, const char* from )
{
	char* cp;

	cp = to;

	while( *cp )
		cp++;

	while( (*cp++ = *from++) )
		continue;

	return to;
}

unsigned char ItsNumber ( unsigned char val )  
{
	if ( val >= '0' && val <= '9' )	/*if parameter is output number */
	{
		return TRUE;
	}

	return FALSE;
}	

unsigned char* HexToChar( unsigned char* Char, unsigned char* Hex, unsigned char HexCount )
{
	unsigned char temp;
	//errno = 0;

	while ( HexCount-- )
	{ 
		temp = *Hex >> 4;
		if ( temp >= 10 )
			temp = temp + 'A' -10;  
		else
			temp += '0';

		*Char++ = temp;  
	
		temp = *Hex & 0x0F;
		if ( temp >= 10 )
			temp = temp + 'A' -10;
		else
			temp += '0';

		*Char++ = temp;

		Hex++;

	}

	*Char = 0;
	return Char;  
}

char* LongToStr( unsigned char* s, unsigned long Val  )
{
	unsigned char Digit = 0, i = 8, p = 0;//, begin = 0;

	while( 1)
	{
		while ( Val >= Exp[i] )
		{
			Val -= Exp[i];
			Digit++;
			//begin++;
		}

		//if ( Digit || begin )
		if ( Digit || p )
			s[p++] = Digit + '0';

		Digit = 0;

		if ( i-- == 0 )
			break;
	}
	
	s[p++] = Val + '0';
	s[p] = 0;

	return s; 
}

void MemSet ( unsigned char *m, unsigned char ByteNumber, unsigned char FillByte )
{
	unsigned char i;

	for ( i = 0; i < ByteNumber; i++ )
	{
		m[i] = FillByte; 
	} 
} 

unsigned char* StrCpy ( unsigned char *s1, const char *s2 )
{

	while( *s1++ = *s2++ )
	{
		;
	}

	return s1;
}

int StrLen( const char *s )
{
	int Length = 0;
	
	while ( *s++ )
	{
		if ( ++Length > 100 )
			return ( -1); 
	}
	
	return Length; 
}

unsigned char* CharnToHex( unsigned char* Hex, unsigned char* Char, unsigned char ByteCount )
{
	unsigned char temp;

	//errno = 0;
	
	while ( *Char >= ' ' )
	{
		temp = *Char++;
		if ( temp >= 'A' )
			temp = (temp - 'A') + 10;
		else
			temp -= '0';

		*Hex = temp<<4;

		temp = *Char++;
		if ( temp >= 'A' )
			temp = (temp - 'A') + 10;
		else
			temp -= '0';
		
	   *Hex |= temp;
		
//		errno++;
		Hex++;

		if ( --ByteCount == 0 )
			break;
	}

	return Hex;
}

signed char StrCmp( register const char * s1, register const char * s2 )
{
	signed char Result;
	
	
	while( !(Result = (unsigned char)*s1 - (unsigned char)*s2++) && *s1++ )
		continue;


	return Result;
}
